package org.lemon.cla.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.lemon.cla.pojo.ClubActivity;
import org.lemon.cla.pojo.User; // Import User entity
import org.lemon.cla.mapper.ClubActivityMapper;
import org.lemon.cla.service.IClubActivityService;
import org.lemon.cla.service.IUserService; // Import IUserService
import org.lemon.cla.dto.request.ClubActivitySearchRequest;
import org.lemon.cla.vo.ClubActivityVo; // Import ClubActivityVo
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ClubActivityServiceImpl extends ServiceImpl<ClubActivityMapper, ClubActivity> implements IClubActivityService {

    @Autowired
    private IUserService userService; // Inject IUserService to check user status

    // Helper method to process organizer information in ClubActivityVo
    private ClubActivityVo processOrganizerInfo(ClubActivityVo activityVo) {
        if (activityVo == null) {
            return null;
        }
        // If organizerId is null (e.g., if ON DELETE SET NULL was active on a past physical delete),
        // or if organizerIsDeleted is true (meaning the user is logically deleted)
        if (activityVo.getOrganizerId() == null || (activityVo.getOrganizerIsDeleted() != null && activityVo.getOrganizerIsDeleted())) {
            activityVo.setOrganizerUsername("未知用户");
            activityVo.setOrganizerAvatarUrl(null);
            activityVo.setOrganizerRealName("未知");
            activityVo.setOrganizerPhone(null);
            activityVo.setOrganizerEmail(null);
            activityVo.setOrganizerRole(null); // Or set to a default "unknown" role enum
        }
        return activityVo;
    }

    // Helper method to process organizer information for a paginated result
    private IPage<ClubActivityVo> processOrganizerInfoForPage(IPage<ClubActivityVo> page) {
        if (page != null && page.getRecords() != null) {
            page.getRecords().forEach(this::processOrganizerInfo);
        }
        return page;
    }

    @Override
    public ClubActivityVo getClubActivityVoById(Long activityId) {
        ClubActivityVo activityVo = baseMapper.selectActivityVoById(activityId);
        return processOrganizerInfo(activityVo); // Process single ClubActivityVo
    }

    @Override
    public IPage<ClubActivityVo> getAllClubActivitiesVo(Page<ClubActivityVo> page) {
        IPage<ClubActivityVo> activityPage = baseMapper.selectActivityVoPage(page);
        return processOrganizerInfoForPage(activityPage); // Process paginated result
    }

    @Override
    public IPage<ClubActivityVo> searchClubActivitiesByConditions(Page<ClubActivityVo> page, ClubActivitySearchRequest request) {
        IPage<ClubActivityVo> resultPage = baseMapper.selectActivityVoByMultiConditions(page, request);
        return processOrganizerInfoForPage(resultPage); // Process paginated result
    }
}